package com.jiamao.goplay.ui.settings;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.FeedbackLayoutBinding;
import com.jiamao.goplay.ui.settings.vm.FeedBackVM;

import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 意见反馈页
 */
public class FeedBackFragment extends BaseMVVMFragment<FeedbackLayoutBinding, FeedBackVM> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.feedback_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public FeedBackVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(FeedBackVM.class);
    }

    @Override
    public void initData() {
        super.initData();

        TextView textViewTitle = binding.getRoot().findViewById(R.id.tvTitleReport);
        textViewTitle.setText("意见与反馈");
        binding.getRoot().findViewById(R.id.ll_back_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        //提交意见反馈
        binding.getRoot().findViewById(R.id.ll_submit_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (binding.inputContent.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请输入意见与反馈内容");
                } else {
                    ToastUtils.showShort(binding.inputContent.getText().toString());
                    startContainerActivity(AccountSafeFragment.class.getCanonicalName());
                }
            }
        });

        //输入字数统计
        binding.inputContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                binding.maxLenFeedback.setText(String.format("%s/200", editable.length()));
            }
        });
    }
}
